<?php

namespace App\Http\Controllers\Check\Feishu;

use App\Enums\Check\CheckEnum;
use App\Http\Controllers\BaseController;
use App\Service\Check\CheckStreamParam;
use App\Service\Check\CheckStreamService;
use Illuminate\Http\Request;

class CheckController extends BaseController
{
    public function enums(Request $request)
    {
        return $this->success(CheckEnum::apiEnums());
    }

    public function streamLists(Request $request): \Illuminate\Http\JsonResponse
    {
        $startTime   = (string)$request->get('startTime');
        $endTime     = (string)$request->get('endTime');
        $sysType     = (int)$request->get('sysType', 0);
        $orderId     = (string)$request->get('orderId', '');
        $payNo       = (string)$request->get('payNo', '');
        $checkStatus = (int)$request->get('checkStatus', 0);
        $payChannel  = (int)$request->get('payChannel', 0);
        $payAction   = (int)$request->get('payAction', 0);
        $pageSize    = (int)$request->get('pageSize', 20);
        $pageNum     = (int)$request->get('pageNum', 1);
        $export      = (int)(bool)$request->get('export', 0);

        if ($export) {
            $pageNum  = 1;
            $pageSize = 99999;
        }

        $startTime = strtotime($startTime);
        $endTime   = strtotime($endTime);
        if (!$startTime) {
            $startTime = strtotime(date('Ymd')) - 86400;
        }

        if (!$endTime) {
            $endTime = strtotime(date('Ymd')) - 1;
        }

        $param = (new CheckStreamParam())
            ->setSysTypes([$sysType])
            ->setStartTime($startTime)
            ->setEndTime($endTime)
            ->setOrderId($orderId)
            ->setPayNo($payNo)
            ->setPageSize($pageSize)
            ->setPageNum($pageNum)
            ->setCheckStatus($checkStatus)
            ->setPayChannel($payChannel)
            ->setPayAction($payAction)
            ->setExport($export);

        return $this->success((new CheckStreamService($param))->streamLists());
    }

    public function dayLists(Request $request)
    {
        $startTime = (string)$request->get('startTime');
        $endTime   = (string)$request->get('endTime');
        $sysType   = (int)$request->get('sysType', 0);
        $export    = (int)(bool)$request->get('export', 0);

        $startTime = strtotime($startTime);
        $endTime   = strtotime($endTime);
        if (!$startTime) {
            $startTime = strtotime(date('Ymd')) - 7 * 86400;
        }

        if (!$endTime) {
            $endTime = strtotime(date('Ymd')) - 1;
        }

        // 对账周期不超过31天
        if (($endTime + 1 - $startTime) / 86400 > 31) {
            return $this->fail(400, '对账周期不超过31天');
        }

        $param = (new CheckStreamParam())
            ->setSysTypes([$sysType])
            ->setStartTime($startTime)
            ->setEndTime($endTime)
            ->setExport($export);

        return $this->success((new CheckStreamService($param))->dayLists());
    }
}
